package com.frxuexi.video

import androidx.lifecycle.MutableLiveData
import com.frxuexi.lib_base.db.CommentDao
import com.frxuexi.lib_base.db.VideoDao
import com.frxuexi.lib_base.db.ZanDao
import com.frxuexi.lib_base.entity.Comment
import com.frxuexi.lib_base.entity.CommentBean
import com.frxuexi.lib_base.entity.CommentBeanItem
import com.frxuexi.lib_base.entity.VideoBean
import com.frxuexi.lib_base.entity.VideoData
import com.frxuexi.lib_base.entity.VideoZanBean
import com.frxuexi.lib_base.mvvm.BaseViewModel
import com.frxuexi.lib_base.net.VideoApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import javax.inject.Inject

@HiltViewModel
class VideoViewModel @Inject constructor(val videoApiService: VideoApiService,val commentDao: CommentDao,val zanDao: ZanDao,val videoDao: VideoDao):BaseViewModel(){
    var videosData = MutableLiveData<VideoBean>()

    var addCommentData = MutableLiveData<Long>()

    var commentsData = MutableLiveData<CommentBean>()
    var commentsRoomData = MutableLiveData<List<Comment>>()

    var commentsZanRoomData = MutableLiveData<Int>()

    var addZanData = MutableLiveData<Long>()
    var zanData = MutableLiveData<Int>()
    var deleteZanData = MutableLiveData<Int>()

    var addVideoData = MutableLiveData<Long>()

    var getVideoRoomData = MutableLiveData<VideoData>()
    fun getVideos(page:Int,size:Int){
        httpRequest({videoApiService.getVideos(page,size)},videosData::postValue)
    }

    fun addComment(commentBeanItem: CommentBeanItem){
        httpRequest({videoApiService.addComment(toBody(commentBeanItem))},{
            val bean = Comment(
                it.agreenum,
                it.content,
                it.createtime,
                it.datatype,
                it.id?.toLong(),
                it.itemid,
                it.nickname,
                it.replytotal,
                it.userid,
                it.userlogo,
                false,
                0
            )
            roomRequest({commentDao.addComment(bean)},addCommentData::postValue)
        })
    }

    fun addCommentRoom(bean:Comment){
        roomRequest({commentDao.addComment(bean)},
            addCommentData::postValue
        )
    }

    fun getComments(id:Int?){
        httpRequest({videoApiService.getComment(id)},commentsData::postValue)
    }

    fun getCommentsRoom(id:Int){
        roomRequest({commentDao.getComments(id)},commentsRoomData::postValue)
    }

    fun commentsZanRoom(bean: Comment){
        roomRequest({commentDao.zan(bean)},commentsZanRoomData::postValue)
    }

    fun addZan(bean: VideoZanBean){
        roomRequest({zanDao.addZan(bean)},addZanData::postValue)
    }

    fun getZan(vid:Int?){
        roomRequest({zanDao.getZan(vid)},zanData::postValue)
    }
    fun deleteZan(bean: VideoZanBean){
        roomRequest({zanDao.deleteZan(bean)},deleteZanData::postValue)
    }

    fun addVideo(bean: VideoData){
        roomRequest({videoDao.addVideo(bean)},addVideoData::postValue)
    }

    fun getVideoRoom(){
        roomRequest({videoDao.getLastVideo()},getVideoRoomData::postValue)
    }


}